package com.yunwaikeji.gy_tool_library.utils;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;

import com.yunwaikeji.gy_tool_library.info.GYTextViewInfo;
import com.yunwaikeji.gy_tool_library.interface1.TextChangedListener;
import com.yunwaikeji.gy_tool_library.view.GYTextView;
import com.yunwaikeji.gy_tool_library.widget.GYEditText;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYTextUtils implements GYTextViewInfo {
    public static GYTextUtils gyTextUtils;

    public static GYTextUtils getInstance() {
        if (gyTextUtils == null) {
            gyTextUtils = new GYTextUtils();
        }
        return gyTextUtils;
    }

    public static List<GYTextView> getAllTextViewList(View view) {
        List<GYTextView> textViewList = new ArrayList<>();
        if (view instanceof GYTextView) {
            textViewList.add((GYTextView) view);
        } else if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                textViewList.addAll(getAllTextViewList(viewGroup.getChildAt(i)));
            }
        }
        return textViewList;
    }

    public static List<GYEditText> getAllEditViewList(View view) {
        List<GYEditText> editViewList = new ArrayList<>();
        if (view instanceof GYEditText) {
            editViewList.add((GYEditText) view);
        } else if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                editViewList.addAll(getAllEditViewList(viewGroup.getChildAt(i)));
            }
        }
        return editViewList;
    }

    public static void setTextChangedListener(TextChangedListener textChangedListener, List<? extends GYTextView> textViews) {
        if (textChangedListener != null) {
            for (GYTextView textView : textViews) {
                textView.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        boolean fitLength = true;
                        for (GYTextView gyTextView : textViews) {
                            if (!gyTextView.getViewSet().fitLength()) {
                                fitLength = false;
                                break;
                            }
                        }
                        textChangedListener.textChanged(textView, fitLength);
                    }
                });
            }
        }
    }

    public static void setTextChangedListener(TextChangedListener textChangedListener, GYTextView... textViews) {
        setTextChangedListener(textChangedListener, Arrays.asList(textViews));
    }

    public GYTextUtils setTextLengthChangedListener(TextLengthChangedListener textLengthChangedListener, GYTextView... gyTextViews) {
        return setTextLengthChangedListener(textLengthChangedListener, Arrays.asList(gyTextViews));
    }

    public GYTextUtils setTextLengthChangedListener(TextLengthChangedListener textLengthChangedListener, List<? extends GYTextView> gyTextViewList) {
        if (textLengthChangedListener != null) {
            for (GYTextView gyTextView : gyTextViewList) {
                gyTextView.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        boolean isLength = true;
                        for (GYTextView gyTextView : gyTextViewList) {
                            if (!gyTextView.getViewSet().fitLength()) {
                                isLength = false;
                                break;
                            }
                        }
                        textLengthChangedListener.textLengthChange(isLength);
                    }
                });
            }
        }
        return this;
    }

    public interface TextLengthChangedListener {
        void textLengthChange(boolean isLength);
    }

    public int getLength(Object object) {
        int length = 0;
        if (object != null) {
            String allString = String.valueOf(object);
            if (!TextUtils.isEmpty(allString)) {
                for (int i = 0; i < allString.length(); i++) {
                    char oldChar = allString.charAt(i);
                    if (oldChar >= 32 && oldChar <= 122) {
                        length++;
                    } else {
                        length += 2;
                    }
                }
            }
        }
        return length;
    }

    public String getLengthString(String text, int maxLength) {
        String lengthString = text;
        if (!TextUtils.isEmpty(text) && getLength(text) > maxLength) {
            int length = 0;
            for (int i = 0; i < text.length(); i++) {
                char oldChar = text.charAt(i);
                if (oldChar >= 32 && oldChar <= 122) {
                    length++;
                } else {
                    length += 2;
                }
                if (length > maxLength) {
                    lengthString = text.substring(0, i);
                    break;
                }
            }
        }
        return lengthString;
    }
}
